<?php 
/**
 * @file 
 * This file is for configure payout ammounts
 */

require_once("security.php");	

if (@$_GET['q'] == "editrow")
	{
	$query = sprintf("update shipValues set value = %s, category = '%s' where ship = '%s';", 
		mysql_real_escape_string($_POST['value']), 
		mysql_real_escape_string($_POST['category']), 
		mysql_real_escape_string($_POST['ship']));
	mysql_query($query, $conn);
	exit;
	}

if (@$_GET['submit'] == "update")
	{
	$query = sprintf("delete from shipValues where ship = '%s'", mysql_real_escape_string($_GET['ship']));
	mysql_query($query, $conn);	
	$query = sprintf("insert into shipValues (ship, value) values ('%s', %d)", mysql_real_escape_string($_GET['ship']), mysql_real_escape_string($_GET['value']));
	mysql_query($query, $conn);	
	
	header("location:configure.php");	
	}

require("header.php");
require("admin_header.php");
?>

<br><br>

<?php 
$res = mysql_query("select a.ship, b.value from (select ship from losses where status = 2 group by ship) as a left join shipValues as b on a.ship = b.ship where value is null and a.ship != 'Capsule';", $conn);
if (mysql_num_rows($res) != 0)
	{
?>
<table width=80%>
<tr><td>The following ships have been submitted for reimbursment but do not have a value.   
Ships without a value will not appear on the review interface.
<tr><td><center><form><table><tr><td>
<select name=ship>
<?php 
while($row = mysql_fetch_assoc($res))
	{
	echo("<option>".$row['ship']."</option>");	
	}
?>
</select>
<td><input name=value><td><input type=submit name=submit value=update>
</table>
<br>
</table>
<?php } ?>

<script type="text/javascript">

var mydata = [<?php 
		$res = mysql_query("select * from shipValues order by ship;", $conn);
		if (!$res)
			die("Problem with mysql: ".$message);

		while($row = mysql_fetch_assoc($res))
			{
			echo("{category:\"".$row['category']."\", ship:\"".$row['ship']."\", value: \"".$row['value']."\"},");
			}
		?>];

		var lastsel; 

		$(document).ready(function() { 
			$("#list").jqGrid({
		    	data:mydata,
		      	url:'example.php',
		      	datatype: 'local',
		      	mtype: 'GET',
		      	colNames:['Ship', 'Value', 'Category'],
		      	colModel :[		      
		        	{name:'ship', index:'ship'}, 
		        	{name:'value', index:'value', editable:true, formatter:'currency', formatoptions: {thousandsSeparator: ","}, sorttype: 'number'},
		        	{name:'category', index:'category', editable:true, edittype:"select",editoptions:{value:"Clones:Clones;Frigates:Frigates;Destroyers:Destroyers;Cruisers:Cruisers;Battlecruisers:Battlecruisers;Battleships:Battleships;Capitals:Capitals;Supercapitals:Supercapitals;Industrials:Industrials;Logistics:Logistics;Interceptors:Interceptors;Interdictors:Interdictors;Stealth Bombers:Stealth Bombers;Recons:Recons;HACs:HACs;Hvy Dictors:Hvy Dictors;Covert Ops:Covert Ops;Unknown:Unknown"}},   
		      		],
		      	pager: '#pager',
		      	rowNum:1000,
		      	height: 'auto', 
		      	width: 750,
		      	rowList:[10,20,30,1000],
		      	sortname: 'ship',
		      	sortorder: 'asc',
		      	viewrecords: true,
		      	caption: 'Ship Manager',
		      	grouping:true, 
		      	groupingView : { groupField : ['category'], groupCollapse : true },
		      	onSelectRow: function(id){ 
		      		if(id && id!==lastsel)
		          		{ 
		          		jQuery('#list').jqGrid('saveRow',lastsel, false, "configure.php?q=editrow", {ship:jQuery("#list").jqGrid('getRowData',lastsel).ship}); 
		          		jQuery('#list').jqGrid('editRow', id, false); 
		          		lastsel=id; 
		          		} 
		      		},           
		    	}); 

			//jQuery("#list").jqGrid('navGrid',"#pager",{edit:false,add:false,del:true}); 
			//jQuery("#list").jqGrid('editRow',"Drachs"); 
		  	}); 

</script>

<br>
<table width=80%>
<tr><td>The following ships have values in the system.  To edit values, select a row, change the appropriate values, and then select a different row to save.
<br><br>
<table id="list"></table> 
<div id="pager"></div> 

</table>

